<template>
  <div class="container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" >
        <template v-if="action&&action=='edit'">
          <el-input v-model="ruleForm.password" :disabled="disabledPWD" show-password clearable>
            <el-checkbox slot="append" @change="setPWD">修改密码</el-checkbox>
          </el-input>
        </template>
        <template v-else>
          <el-input v-model="ruleForm.password" show-password clearable></el-input>
        </template>
      </el-form-item>
      <el-form-item label="角色" prop="role" align="left">
        <el-radio-group v-model="ruleForm.role">
          <el-radio label="管理员"></el-radio>
          <el-radio label="超级管理员"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
        role: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {pattern: /[a-zA-Z0-9_]{6,15}$/, message: '密码必须为6-15位的字母数字或下划线！', trigger: 'blur'}
        ],
        role: [
          {required: true, message: '请选择角色', trigger: 'change'}
        ]
      },
      action: '',
      disabledPWD:true
    };
  },
  created() {
    const {info, action} = this.$route.query
    if (action && action == 'edit') {
        this.action = 'edit'
        if(info) {
          this.ruleForm = JSON.parse(info)
        }
        // console.log(this.ruleForm)
    }else{
      this.action='add'
    }
  },
  methods: {
    //在修改跳转过来后 设置可以修改密码
    setPWD(){
      this.disabledPWD=!this.disabledPWD
      if(this.disabledPWD){
        this.rules.password=[]
        console.log("...")
        this.$refs.ruleForm.resetFields()
      }else{
        this.rules.password= [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {pattern: /[a-zA-Z0-9_]{6,15}$/, message: '密码必须为6-15位的字母数字或下划线！', trigger: 'blur'}
        ]
      }
    },
    submitForm(formName) {
      if(this.disabledPWD){
        this.rules.password=[]
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm(`您确定保存该操作吗?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async() => {
            // 确定操作
           if(this.action=='add'){
             const result= await this.$http.post("account/save",this.ruleForm)
             if(result.data.status=='success'){
               this.$message.success("添加成功,可以继续添加...")
               this.ruleForm={
                 username: '',
                 password: '',
                 role: ''
               }
             }else{
               this.$message.error("添加失败,请稍后重试...")
               this.$router.push('admin')
             }
           }else if(this.action=='edit'){
             if(this.disabledPWD){
               this.ruleForm.password="";
             }
             const result= await this.$http.put("account/edit",this.ruleForm)
             if(result.data.status=='success'){
               this.$message.success("修改成功")
             }else{
               this.$message.error("修改失败或没有修改,请稍后重试...")
             }
             this.$router.push('admin')
           }
          }).catch(() => {
            // 取消操作
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  background-color: #fff;
  padding: 10px;

  .el-form {
    margin-top: 20px;
    width: 50%;
  }
}
</style>